//evaluateCMD.cpp

#include "evaluateCMD.h"
#include <iostream>
#include <string>
#include <sstream>

evaluateCMD::COMMAND_TYPE evaluateCMD::determineCommand(std::string command) {
	if( command == CMD_ADD) {
		return COMMAND_TYPE::ADD;
	} else if ((command==CMD_DELETE)) {
		return COMMAND_TYPE::DELETE;
	} else if ((command== CMD_DISPLAY)) {
		return COMMAND_TYPE::DISPLAY;
	} else if ((command== CMD_EDIT)) {
		return COMMAND_TYPE::EDIT;
	} else if ((command== CMD_CLEAR)) {
		return COMMAND_TYPE::CLEAR;
	} else if ((command== CMD_EXIT)) {
		return COMMAND_TYPE::EXIT;
	} else if ((command== CMD_SEARCH)) {
		return COMMAND_TYPE::SEARCH;
	} else if ((command== CMD_SORT)) {
		return COMMAND_TYPE::SORT;
	} else if ((command== CMD_UNDO)) {
		return COMMAND_TYPE::UNDO;
	} else if ((command== CMD_REDO)) {
		return COMMAND_TYPE::REDO;
	}else if ((command== CMD_COMPLETE)) {
		return COMMAND_TYPE::COMPLETE;
	}else {
		return COMMAND_TYPE::INVALID;
	}
}

std::string evaluateCMD::executeCommand(std::string taskName, int taskDate, int taskIndex) {
	std::string command;
	_inputCmd >> command;
	switch(determineCommand(command)) {
	case ADD:
		createTask(taskName, taskDate);
		return    ;
	case DELETE:
		deleteTask(taskIndex);
		return ;
	case DISPLAY:
		displayTask();
		return ;
	case EDIT:
		updateTask(taskIndex, taskName, taskDate);
		return ;
	case CLEAR:
		clearTask();
		return ;
	case UNDO:
		undoTask();
		return ;
	case REDO:
		redoTask();
		return ;
	case COMPLETE:
		completeTask();
		return ;
	case EXIT:
		return ;
	case SORT:
		sortTask();
		return ;
	case SEARCH:
		searchTask();
		return ;
	case INVALID:
		std::cout << "Invalid command. Please enter another command: ";
		return ;
	}
}

std::Task evaluateCMD::splitAddCMD(std::string userInput, std::string taskName, int startTime, int endTime, int deadline ){
	unsigned int tStart = 0, tEnd = 0;
	while( tEnd != std::string::npos ) {
		std::string temp = userInput;
		tStart = temp.find(KEYWORD_ADD);
		tStart = tStart + 3;
		tEnd = temp.find(KEYWORD_FROM);
		taskName = temp.substr(tStart, tEnd - tStart);	

		std::string temp = userInput;
		tStart = temp.find(KEYWORD_FROM);
		tStart = tStart + 4;
		tEnd = temp.find(KEYWORD_TO);
		std::string startTimestring = temp.substr(tStart, tEnd - tStart);	
		std::stringstream str( startTimestring );
		str >> startTime;

		std::string temp = userInput;
		tStart = temp.find(KEYWORD_TO);
		tStart = tStart + 2;
		tEnd = temp.find(KEYWORD_ON);
		std::string endTimestring = temp.substr(tStart, tEnd - tStart);	
		std::stringstream str( endTimestring );
		str >> endTime;

		std::string temp = userInput;
		tStart = temp.find(KEYWORD_ON);
		tStart = tStart + 2;
		tEnd = temp.find_first_of(DELIMITER_END);
		std::string deadlinestring = temp.substr(tStart, tEnd - tStart);	
		std::stringstream str( deadlinestring );
		str >> deadline;
	}
}


std::Task evaluateCMD::splitEditCMD(std::string userInput, int taskIndex, std::string attributeType, int attribute, std::string taskName, int startTime, int endTime, int deadline){
	unsigned int tStart = 0, tEnd = 0;
	while( tEnd != std::string::npos ) {
		std::string temp = userInput;
		tStart = temp.find_first_of(" ");
		tEnd = temp.find(" ", tStart+1);
		std::string attributestring = temp.substr(tStart, tEnd - tStart);	
		std::stringstream str( attributestring );
		str >> attribute;
		taskIndex = attribute;

		std::string temp = userInput;
		tStart = tEnd + 1;
		tEnd = temp.find(" ", tStart);
		attributeType = temp.substr(tStart, tEnd - tStart);

		std::string temp = userInput;
		tStart = tEnd + 1;
		tEnd = temp.find(DELIMITER_SPACE, tStart);

		if(tEnd == std::string::npos){
			tEnd = temp.find(DELIMITER_END, tStart);
		}

		if(attributeType == "start"){
			std::string attributestring = temp.substr(tStart, tEnd - tStart);	
			std::stringstream str( attributestring );
			str >> attribute;
			startTime = attribute;

			if( tEnd != std::string::npos ) {        //if user want to update both start and end time
				std::string temp = userInput;
				tStart = temp.find("DELIMITER_SPACE", tEnd+1);
				tEnd = temp.find('\0', tStart);
				std::string attributestring = temp.substr(tStart, tEnd - tStart);
				std::stringstream str( attributestring );
				str >> attribute;
				endTime = attribute;
			}
		} else if(attributeType == "end"){
			std::string attributestring = temp.substr(tStart, tEnd - tStart);	
			std::stringstream str( attributestring );
			str >> attribute;
			endTime = attribute;
		} else if(attributeType == "deadline"){
			std::string attributestring = temp.substr(tStart, tEnd - tStart);	
			std::stringstream str( attributestring );
			str >> attribute;
			deadline = attribute;
		} else if(attributeType == "taskname"){
			std::string attributestring = temp.substr(tStart, tEnd - tStart);	
			taskName = attributestring;
		}
	}
}




bool evaluateCMD::isValidLine(int lineNo) {
	//function call to logic to check? or shd it be here? 
	//if we want to put it here, we have to add an additional private attribute, ie. like a vector and then
	//we need to load the file
}


const std::string evaluateCMD::CMD_ADD = "add";
const std::string evaluateCMD::CMD_DELETE = "delete";
const std::string evaluateCMD::CMD_DISPLAY = "display";
const std::string evaluateCMD::CMD_EDIT = "edit";
const std::string evaluateCMD::CMD_CLEAR = "clear";
const std::string evaluateCMD::CMD_EXIT = "exit";
const std::string evaluateCMD::CMD_SORT = "sort";
const std::string evaluateCMD::CMD_SEARCH = "search";
const std::string evaluateCMD::CMD_UNDO ="undo";
const std::string evaluateCMD::CMD_REDO ="redo";
const std::string evaluateCMD::CMD_COMPLETE ="complete";
const std::string evaluateCMD::DELIMITER_SLASH ="/";
const std::string evaluateCMD::DELIMITER_SPACE =" ";
const std::string evaluateCMD::DELIMITER_END ="\n";

const std::string evaluateCMD::KEYWORD_ADD ="add";
const std::string evaluateCMD::KEYWORD_FROM ="from";
const std::string evaluateCMD::KEYWORD_TO ="to";
const std::string evaluateCMD::KEYWORD_ON ="on";

